<script lang="ts" setup>
import { watch } from 'vue'
import { detailSchema } from '@/views/wages/salarySlip/salarySlip.data'
import { BasicForm, useForm } from '@/components/Form'
import type { salarySlipDetailVO } from '@/api/wages/salaryslip/type'

const props = defineProps({
  detailInfo: {
    type: Object as PropType<Nullable<salarySlipDetailVO>>,
    default: () => null,
  },
  isUpdate: {
    type: Boolean,
    default: false,
  },
})

const emit = defineEmits(['next', 'prev'])

const [register, { setFieldsValue, resetFields, validate }] = useForm({
  labelWidth: 120,
  schemas: detailSchema,
  actionColOptions: {
    span: 14,
  },
  showActionButtonGroup: false,
  showResetButton: false,
  submitFunc: customSubmitFunc,
})

async function customSubmitFunc() {
  try {
    const values = await validate()
    emit('next', values)
  }
  catch (error) {
  }
}

async function customResetFunc() {
  emit('prev')
}

watch(
  () => props.detailInfo,
  (detailInfo) => {
    if (!detailInfo)
      return
    resetFields()
    setFieldsValue({ ...detailInfo })
  },
  {
    deep: true,
    immediate: true,
  },
)
</script>

<template>
  <div>
    <div class="mx-auto my-0 w-80%">
      <BasicForm @register="register" />
    </div>
    <div class="flex justify-center">
      <a-button @click="customResetFunc">
        上一步
      </a-button>
      <div style="padding-left: 20px">
        <a-button type="primary" @click="customSubmitFunc">
          保存
        </a-button>
      </div>
    </div>
  </div>
</template>
